package com.sanchunhui.psc.presenter


import com.sanchunhui.psc.R
import com.sanchunhui.psc.data.protocol.ResponseInfo
import com.sanchunhui.psc.ext.excute
import com.sanchunhui.psc.presenter.view.MyTeamMemberView
import com.sanchunhui.psc.presenter.view.ProjectTeamsView
import com.sanchunhui.psc.presenter.view.TeamMembersView
import com.sanchunhui.psc.rx.BaseSubscriber
import com.sanchunhui.psc.service.ProjectTeamService
import javax.inject.Inject

/**
 * @Author zyx
 * @Date 2021/4/20-10:45
 * @Dscribe 项目下所有分组
 */
class ProjectTeamsPresenter @Inject constructor() : BasePresenter<ProjectTeamsView>() {

    @Inject
    lateinit var teamService: ProjectTeamService

    /*
        获取分组列表
     */
    fun getTeamList(token:String) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()
        teamService.getTeamList(token).excute(object : BaseSubscriber<ResponseInfo>(mView){
            override fun onNext(t: ResponseInfo) {
                mView.onGetTeamListResult(t)
            }
        },lifecycleProvider)
    }

    /*
         更改分组状态
     */
    fun changeTeamStatus(teamId: Int,token: String, status:String) {
        if (!checkNetWork()) {
            return
        }
//        mView.showLoading()
        teamService.changeTeamStatus(teamId,token,status).excute(object : BaseSubscriber<Boolean>(mView) {
            override fun onNext(t: Boolean) {
                mView.onChangeTeamStatusResult(t)
            }

        }, lifecycleProvider )
    }

    /*
        获取所在项⽬信息
     */
    fun getProjectInfo(token: String) {
        if (!checkNetWork()) {
            return
        }
        teamService.getProjectInfo(token).excute(object : BaseSubscriber<ResponseInfo>(mView) {
            override fun onNext(t: ResponseInfo) {
                mView.onProjectInfoResult(t)
            }

        }, lifecycleProvider )
    }


    /*
       更改项⽬状态
    */
    fun changeProjectInfo(token: String, status: String) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()
        teamService.changeProjectStatus(token, status).excute(object : BaseSubscriber<ResponseInfo>(mView) {
            override fun onNext(t: ResponseInfo) {
                mView.onChangeProjectStatusResult(t)
            }

        }, lifecycleProvider )
    }

    /*
   *  使用异步获取数据
   * */
    /*
    fun getTeams(tokenStr: String) {
        val tokenReq = TokenReq(tokenStr)
        //TODO:要异步访问
        val observable:Observable<ResponseInfo> = pscService.getTeamList(tokenReq)
//        teamsCall.enqueue(callback)
//        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
//            .subscribe(object :Observer<ResponseInfo>{
//                override fun onError(e: Throwable?) {
//                    if (e!=null) {
//                        Log.e("rxjava",e.localizedMessage)
//                    }
//
//                }
//
//                override fun onNext(responseInfo: ResponseInfo?) {
//                    if (responseInfo!=null) {
//                        parserJson(responseInfo.data)
//                    }
//                }
//
//                override fun onCompleted() {
//
//                }
//            })
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                if (it.code == 0) {
                    val json = it.data
                    parserJson(json.toString())
                } else if (it.code == -1) {
                    //根据具体接口文档表示含义，比如定义-1为空数据
                    Log.e("psc", "定义-1为空数据")
                } else if (it.code == 401) { // 重新登录
                    teamsFragment.onTeamsFailed(it.code)
                }
            },{
                Log.e("rxjava",it.localizedMessage)
            },{
                Log.e("rxjava","onComplete!")
            })
    }

    /*
    * 解析数据
    * */
    fun parserJson(json: String) {
        val gson = Gson()
        var jsonObject = JSONObject(json)
        var teams = jsonObject.getString("teams")
        var temasList : MutableList<Team> = gson.fromJson(teams, object : TypeToken<List<Team>>() {}.type)

        if (temasList.isNotEmpty()) {
            // 有数据，成功页面
//            teamsFragment.onTeamsSuccess(temasList)
        } else {
            // 无数据，异常页面
//            teamsFragment.onTeamsFailed()
        }
    }
    */


}